package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtCustomerService;
import com.ruoyi.business.service.IJtCustomerServiceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 售后预约Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/customer")
public class JtCustomerServiceController extends BaseController
{
    private String prefix = "business/customer";

    @Autowired
    private IJtCustomerServiceService jtCustomerServiceService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("business:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询售后预约列表
     */
    @RequiresPermissions("business:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtCustomerService jtCustomerService)
    {
        startPage();
        List<JtCustomerService> list = jtCustomerServiceService.selectJtCustomerServiceList(jtCustomerService);
        return getDataTable(list);
    }

    /**
     * 导出售后预约列表
     */
    @RequiresPermissions("business:customer:export")
    @Log(title = "客户预约售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtCustomerService jtCustomerService)
    {
        List<JtCustomerService> list = jtCustomerServiceService.selectJtCustomerServiceList(jtCustomerService);
        ExcelUtil<JtCustomerService> util = new ExcelUtil<JtCustomerService>(JtCustomerService.class);
        return util.exportExcel(list, "客户预约售后数据");
    }

    /**
     * 新增售后预约
     */
    @RequiresPermissions("business:customer:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysUser> userList = sysUserService.selectUserByRoleKey("repairer");
        mmap.put("repairList", userList);
        return prefix + "/add";
    }

    /**
     * 新增保存售后预约
     */
    @RequiresPermissions("business:customer:add")
    @Log(title = "售后预约", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JtCustomerService jtCustomerService)
    {
        return toAjax(jtCustomerServiceService.insertJtCustomerService(jtCustomerService));
    }

    /**
     * 修改售后预约
     */
    @RequiresPermissions("business:customer:edit")
    @GetMapping("/edit/{serviceId}")
    public String edit(@PathVariable("serviceId") Long serviceId, ModelMap mmap)
    {
        List<SysUser> userList = sysUserService.selectUserByRoleKey("repairer");
        mmap.put("repairList", userList);
        JtCustomerService jtCustomerService = jtCustomerServiceService.selectJtCustomerServiceByServiceId(serviceId);
        mmap.put("jtCustomerService", jtCustomerService);
        return prefix + "/edit";
    }


    /**
     * 修改保存售后预约
     */
    @RequiresPermissions("business:customer:edit")
    @Log(title = "售后预约", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtCustomerService jtCustomerService)
    {
        return toAjax(jtCustomerServiceService.updateJtCustomerService(jtCustomerService));
    }

    /**
     * 删除售后预约
     */
    @RequiresPermissions("business:customer:remove")
    @Log(title = "售后预约", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtCustomerServiceService.deleteJtCustomerServiceByServiceIds(ids));
    }


    //======================维修人员==============================
    @RequiresPermissions("business:repair:view")
    @GetMapping("/repair")
    public String repair()
    {
        return prefix + "/repair";
    }

    /**
     * 查询售后预约列表
     */
    @RequiresPermissions("business:repair:list")
    @PostMapping("/repair/list")
    @ResponseBody
    public TableDataInfo repairList(JtCustomerService jtCustomerService)
    {
        jtCustomerService.setRepairUserId(ShiroUtils.getUserId());
        startPage();
        List<JtCustomerService> list = jtCustomerServiceService.selectJtCustomerServiceList(jtCustomerService);
        return getDataTable(list);
    }


    /**
     * 修改售后预约
     */
    @RequiresPermissions("business:repair:deal")
    @GetMapping("/repair/deal/{serviceId}")
    public String deal(@PathVariable("serviceId") Long serviceId, ModelMap mmap)
    {
        JtCustomerService jtCustomerService = jtCustomerServiceService.selectJtCustomerServiceByServiceId(serviceId);
        mmap.put("jtCustomerService", jtCustomerService);
        return prefix + "/deal";
    }


    /**
     * 修改保存售后预约
     */
    @RequiresPermissions("business:customer:edit")
    @Log(title = "售后预约-维修结果", businessType = BusinessType.UPDATE)
    @PostMapping("/repair/save")
    @ResponseBody
    public AjaxResult repairSave(JtCustomerService jtCustomerService)
    {
        jtCustomerService.setRepairFinishTime(DateUtils.getNowDate());
        return toAjax(jtCustomerServiceService.updateJtCustomerService(jtCustomerService));
    }
}
